package com.ruoyi.paipan.service.impl;

import com.ruoyi.paipan.common.EightChar;
import com.ruoyi.paipan.common.Lunar;
import com.ruoyi.paipan.common.Solar;
import com.ruoyi.paipan.data.OtherData;
import com.ruoyi.paipan.dto.RequestDto;
import com.ruoyi.paipan.dto.WNLRequestDto;
import com.ruoyi.paipan.service.PaiPanService;
import com.ruoyi.paipan.service.WNLService;
import com.ruoyi.paipan.utils.CommonUtil;
import com.ruoyi.paipan.utils.ToolUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.Cookie;
import java.util.*;

@Service
public class WNLServiceImpl implements WNLService {
    @Autowired
    private PaiPanService paiPanService;
    @Override
    public Object getInfo(RequestDto requestDto, Cookie[] cookies) {
        Integer id = Integer.parseInt(requestDto.getId());
        if(id != 1 && id != 2){
            return Collections.singletonList("参数错误");
        }
        int year = Integer.parseInt(requestDto.getYear());
        String month = requestDto.getMonth();
        String day = requestDto.getDay();
        String hour = requestDto.getHour();
        String minute = requestDto.getMinute();
        String nv = requestDto.getNv();
        String wzs;
        requestDto.setSYLX("wnl");
        paiPanService.getInfo(requestDto,cookies);
        ArrayList<Object> Sc = new ArrayList<>();
        if(id==1){
            Solar d = Solar.fromYmd(year, Integer.parseInt(month), 1);
            int sj = d.getWeek();
            for (int i = 0; i < 42; i++) {
                String cc = d.next(i - sj).toString();
                LinkedHashMap<String, Object> res = new LinkedHashMap<>();
                String[] split = cc.split("-");
                String nn = split[0];
                String mm = split[1];
                String dd = split[2];
                res.put("year",cc);
                res.put("mouth",mm);
                res.put("day",dd);
                Solar rr = Solar.fromYmd(Integer.parseInt(nn), Integer.parseInt(mm), Integer.parseInt(dd));
                res.put("week", rr.getWeek());
                Lunar lunar = rr.getLunar();
                res.put("ngz", lunar.getYearInGanZhiByLiChun());
                res.put("rgz", lunar.getDayInGanZhi());
                String string = lunar.toString();
                res.put("jq", lunar.getJieQi());
                res.put("shengx", string);

                Sc.add(res);
            }
            return Sc;
        }
        if(id == 2){
            LinkedHashMap<String,Object> res = new LinkedHashMap<>();
            nv = Objects.equals(nv, "乾") ? "0" : "1";
            wzs = Objects.equals(nv, "明") ? "1" : "2";
            String zhuanyeban = "vip";
            Solar glslh = Solar.fromYmdHms(year, Integer.parseInt(month), Integer.parseInt(day), Integer.parseInt(hour), Integer.parseInt(minute), 0);
            Lunar nlslh = glslh.getLunar();
            String NLY = nlslh.getMonthInChinese();////农历月
            NLY = NLY.replace("闰", "");
            String NLR = nlslh.getDayInChinese();//农历日
            EightChar baZi = nlslh.getEightChar();//初始化八字
            baZi.setSect(Integer.parseInt(wzs));//初始化流派
            System.out.println(baZi.getYear() + " " + baZi.getMonth() + " " + baZi.getDay() + " " + baZi.getTime());
            String ng = baZi.getYear().substring(0,1);
            String nz = baZi.getYear().substring(baZi.getYear().length()-1);
            String yg = baZi.getMonth().substring(0,  1);
            String yz = baZi.getMonth().substring(baZi.getMonth().length()-1);
            String rg = baZi.getDay().substring(0,  1);
            String rz = baZi.getDay().substring(baZi.getDay().length()-1);
            String sg = baZi.getTime().substring(0,  1);
            String sz = baZi.getTime().substring(baZi.getTime().length()-1);
            LinkedHashMap<String, Object> bazi = new LinkedHashMap<>();
            ArrayList<Object> tiangan = new ArrayList<>();
            List<String> color = Arrays.asList("jin", "mu", "shui", "huo", "tu");
            LinkedHashMap<String, LinkedHashMap<String, LinkedHashMap<String, String>>> System_ZangGan = CommonUtil.System_ZangGan(rg);//藏干遍历
            tiangan.add(new LinkedHashMap<String,String>(){{
                put("text",ng);
                put("color", color.get(Integer.parseInt(OtherData.SX.get(ng).get(0))));
                put("shishen", System_ZangGan.get(ng).get("占").get("全"));
            }});
            tiangan.add(new LinkedHashMap<String,String>(){{
                put("text",yg);
                put("color", color.get(Integer.parseInt(OtherData.SX.get(yg).get(0))));
                put("shishen", System_ZangGan.get(yg).get("占").get("全"));
            }});
            tiangan.add(new LinkedHashMap<String,String>(){{
                put("text",rg);
                put("color", color.get(Integer.parseInt(OtherData.SX.get(rg).get(0))));
                put("shishen", requestDto.getNv() + "造");
            }});
            tiangan.add(new LinkedHashMap<String,String>(){{
                put("text",sg);
                put("color", color.get(Integer.parseInt(OtherData.SX.get(sg).get(0))));
                put("shishen", System_ZangGan.get(sg).get("占").get("全"));
            }});
            bazi.put("tiangan", tiangan);
            ArrayList<Object> dizhi = new ArrayList<>();
            ArrayList<Object> canggan = new ArrayList<>();
            canggan.add(new LinkedHashMap<String,String>(){{
                put("text",OtherData.SX.get(nz).get(3));
                put("color",color.get(Integer.parseInt(OtherData.SX.get(OtherData.SX.get(nz).get(3)).get(0))));
                put("shishen", System_ZangGan.get(nz).get("本").get("全"));
            }});
            if (!OtherData.SX.get(nz).get(4).isEmpty()) {
                canggan.add(new LinkedHashMap<String,String>(){{
                    put("text",OtherData.SX.get(nz).get(4));
                    put("color",color.get(Integer.parseInt(OtherData.SX.get(OtherData.SX.get(nz).get(4)).get(0))));
                    put("shishen", System_ZangGan.get(nz).get("中").get("全"));
                }});
            }
            if (!OtherData.SX.get(nz).get(5).isEmpty()) {
                canggan.add(new LinkedHashMap<String,String>(){{
                    put("text",OtherData.SX.get(nz).get(5));
                    put("color",color.get(Integer.parseInt(OtherData.SX.get(OtherData.SX.get(nz).get(5)).get(0))));
                    put("shishen", System_ZangGan.get(nz).get("余").get("全"));
                }});
            }
            dizhi.add(new LinkedHashMap<String,Object>(){{
                put("text",nz);
                put("color", color.get(Integer.parseInt(OtherData.SX.get(nz).get(0))));
                put("canggan",canggan);
            }});

            ArrayList<Object> canggan1 = new ArrayList<>();
            canggan1.add(new LinkedHashMap<String,String>(){{
                put("text",OtherData.SX.get(yz).get(3));
                put("color",color.get(Integer.parseInt(OtherData.SX.get(OtherData.SX.get(yz).get(3)).get(0))));
                put("shishen", System_ZangGan.get(yz).get("本").get("全"));
            }});
            if (!OtherData.SX.get(yz).get(4).isEmpty()) {
                canggan1.add(new LinkedHashMap<String,String>(){{
                    put("text",OtherData.SX.get(yz).get(4));
                    put("color",color.get(Integer.parseInt(OtherData.SX.get(OtherData.SX.get(yz).get(4)).get(0))));
                    put("shishen", System_ZangGan.get(yz).get("中").get("全"));
                }});
            }
            if (!OtherData.SX.get(yz).get(5).isEmpty()) {
                canggan1.add(new LinkedHashMap<String,String>(){{
                    put("text",OtherData.SX.get(yz).get(5));
                    put("color",color.get(Integer.parseInt(OtherData.SX.get(OtherData.SX.get(yz).get(5)).get(0))));
                    put("shishen", System_ZangGan.get(yz).get("余").get("全"));
                }});
            }
            dizhi.add(new LinkedHashMap<String,Object>(){{
                put("text",yz);
                put("color", color.get(Integer.parseInt(OtherData.SX.get(yz).get(0))));
                put("canggan",canggan1);
            }});

            ArrayList<Object> canggan2 = new ArrayList<>();
            canggan2.add(new LinkedHashMap<String,String>(){{
                put("text",OtherData.SX.get(rz).get(3));
                put("color",color.get(Integer.parseInt(OtherData.SX.get(OtherData.SX.get(rz).get(3)).get(0))));
                put("shishen", System_ZangGan.get(rz).get("本").get("全"));
            }});
            if (!OtherData.SX.get(rz).get(4).isEmpty()) {
                canggan2.add(new LinkedHashMap<String,String>(){{
                    put("text",OtherData.SX.get(rz).get(4));
                    put("color",color.get(Integer.parseInt(OtherData.SX.get(OtherData.SX.get(rz).get(4)).get(0))));
                    put("shishen", System_ZangGan.get(rz).get("中").get("全"));
                }});
            }
            if (!OtherData.SX.get(rz).get(5).isEmpty()) {
                canggan2.add(new LinkedHashMap<String,String>(){{
                    put("text",OtherData.SX.get(rz).get(5));
                    put("color",color.get(Integer.parseInt(OtherData.SX.get(OtherData.SX.get(rz).get(5)).get(0))));
                    put("shishen", System_ZangGan.get(rz).get("余").get("全"));
                }});
            }
            dizhi.add(new LinkedHashMap<String,Object>(){{
                put("text",rz);
                put("color", color.get(Integer.parseInt(OtherData.SX.get(rz).get(0))));
                put("canggan",canggan2);
            }});
            ArrayList<Object> canggan3 = new ArrayList<>();
            canggan3.add(new LinkedHashMap<String,String>(){{
                put("text",OtherData.SX.get(sz).get(3));
                put("color",color.get(Integer.parseInt(OtherData.SX.get(OtherData.SX.get(sz).get(3)).get(0))));
                put("shishen", System_ZangGan.get(sz).get("本").get("全"));
            }});
            if (!OtherData.SX.get(sz).get(4).isEmpty()) {
                canggan3.add(new LinkedHashMap<String,String>(){{
                    put("text",OtherData.SX.get(sz).get(4));
                    put("color",color.get(Integer.parseInt(OtherData.SX.get(OtherData.SX.get(sz).get(4)).get(0))));
                    put("shishen", System_ZangGan.get(sz).get("中").get("全"));
                }});
            }
            if (!OtherData.SX.get(sz).get(5).isEmpty()) {
                canggan3.add(new LinkedHashMap<String,String>(){{
                    put("text",OtherData.SX.get(sz).get(5));
                    put("color",color.get(Integer.parseInt(OtherData.SX.get(OtherData.SX.get(sz).get(5)).get(0))));
                    put("shishen", System_ZangGan.get(sz).get("余").get("全"));
                }});
            }
            dizhi.add(new LinkedHashMap<String,Object>(){{
                put("text",sz);
                put("color", color.get(Integer.parseInt(OtherData.SX.get(sz).get(0))));
                put("canggan",canggan3);
            }});
            bazi.put("dizhi",dizhi);
            ArrayList<String> kongwang = new ArrayList<String>(){{
                add(OtherData.NY.get(ng+nz).get(2)+OtherData.NY.get(ng+nz).get(3));
                add(OtherData.NY.get(yg+yz).get(2)+OtherData.NY.get(yg+yz).get(3));
                add(OtherData.NY.get(rg+rz).get(2)+OtherData.NY.get(rg+rz).get(3));
                add(OtherData.NY.get(sg+sz).get(2)+OtherData.NY.get(sg+sz).get(3));
            }};
            ArrayList<String> nayin = new ArrayList<String>(){{
                add(OtherData.NY.get(ng+nz).get(0));
                add(OtherData.NY.get(yg+yz).get(0));
                add(OtherData.NY.get(rg+rz).get(0));
                add(OtherData.NY.get(sg+sz).get(0));
            }};
            bazi.put("nayin",nayin);
            String ztg = ng + yg + rg + sg;
            String zzg = OtherData.SX.get(nz).get(3) + OtherData.SX.get(nz).get(4) + OtherData.SX.get(nz).get(5) + OtherData.SX.get(yz).get(3) + OtherData.SX.get(yz).get(4) + OtherData.SX.get(yz).get(5) + OtherData.SX.get(rz).get(3) + OtherData.SX.get(rz).get(4) + OtherData.SX.get(rz).get(5) + OtherData.SX.get(sz).get(3) + OtherData.SX.get(sz).get(4) + OtherData.SX.get(sz).get(5);
            List<Object> System_XS = CommonUtil.System_XS(ng, nz, yg, yz, rg, rz, sg, sz, Integer.valueOf(nv), OtherData.NY, OtherData.SX, OtherData.LLXS, zhuanyeban, ztg, zzg);
            ArrayList<Object> shensha = new ArrayList<Object>(){{
                add(System_XS.get(0));
                add(System_XS.get(1));
                add(System_XS.get(2));
                add(System_XS.get(3));
            }};
            bazi.put("kongwang",kongwang);
            bazi.put("shensha",shensha);
            bazi.put("qiangruo",new ArrayList<Object>(){{
                add(CommonUtil.System_DeLing(OtherData.SX, ng, nz, yg, yz, rg, rz, sg, sz)[5]);
            }});
            bazi.put("tianganhechong",new ArrayList<Object>(){{
                add(CommonUtil.tgdzly(ng + yg + rg + sg));
            }});
            bazi.put("dizhihechong",new ArrayList<Object>(){{
                add(CommonUtil.tgdzly(nz + yz + rz + sz));
            }});

            ArrayList<String> CGG = new ArrayList<>(CommonUtil.System_CGG(OtherData.SQL_CGG, ng, nz, NLY, NLR, sz, Integer.parseInt(nv)));
            CGG.set(0, CGG.get(0)+" "+(CGG.size()>2 ? CGG.get(2):""));
            if(CGG.size()>2) CGG.remove(2);
            bazi.put("chenggu",CGG);
            bazi.put("geju",new ArrayList<Object>(){{
                add(ToolUtil.GeJuFenXi(System_ZangGan, OtherData.SX, ng, nz, yg, yz, rg, rz, sg, sz));
            }});
            String syqq = glslh.next(-283).toString();
            int i = syqq.indexOf("-");
            syqq = syqq.substring(0,i)+"年"+syqq.substring(i+1);
            i = syqq.indexOf("-");
            syqq = syqq.substring(0,i)+"月"+syqq.substring(i+1);
            String syqh = glslh.next(-279).toString();
            i = syqh.indexOf("-");
            syqh = syqh.substring(0,i)+"年"+syqh.substring(i+1);
            i = syqh.indexOf("-");
            syqh = syqh.substring(0,i)+"月"+syqh.substring(i+1);
            String finalSyqq = syqq;
            String finalSyqh = syqh;
            bazi.put("syqh",new ArrayList<String>(){{
                add("建议受孕时间:" + finalSyqq + "日-" + finalSyqh + "日");
            }});
            res.put("bazi",bazi);
            return res;
        }
        return null;
    }
}
